Completed
Push — master ( 4bb4df...375c4e )
by Sander
10s
created

$(document).ready   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
/**
2
 * Nextcloud - passman
3
 *
4
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
5
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
$(document).ready(function () {
24
	var Settings = function (baseUrl) {
25
		this._baseUrl = baseUrl;
26
		this._settings = [];
27
	};
28
29
	Settings.prototype = {
30
		load: function () {
31
			var deferred = $.Deferred();
32
			var self = this;
33
			$.ajax({
34
				url: this._baseUrl,
35
				method: 'GET',
36
				async: false
37
			}).done(function (settings) {
38
				self._settings = settings;
39
			}).fail(function () {
40
				deferred.reject();
41
			});
42
			return deferred.promise();
43
		},
44
45
		setUserKey: function (key, value) {
46
			var request = $.ajax({
47
				url: this._baseUrl + '/' + key + '/' + value,
48
				method: 'POST'
49
			});
50
			request.done(function () {
51
				$('.msg-passwords').removeClass("msg_error");
52
				$('.msg-passwords').text('');
53
			});
54
			request.fail(function () {
55
				$('.msg-passwords').addClass("msg_error");
56
				$('.msg-passwords').text(t('passwords', 'Error while saving field') + ' ' + key + '!');
57
			});
58
		},
59
60
		setAdminKey: function (key, value) {
61
			var request = $.ajax({
62
				url: this._baseUrl + '/' + key + '/' + value + '/admin1/admin2',
63
				method: 'POST'
64
			});
65
			request.done(function () {
66
				$('.msg-passwords').removeClass("msg_error");
67
				$('.msg-passwords').text('');
68
			});
69
			request.fail(function () {
70
				$('.msg-passwords').addClass("msg_error");
71
				$('.msg-passwords').text(t('passwords', 'Error while saving field') + ' ' + key + '!');
72
			});
73
		},
74
		getKey: function (key) {
75
			if (this._settings.hasOwnProperty(key)) {
76
				return this._settings[key];
77
			}
78
			return false;
79
		},
80
		getAll: function () {
81
			return this._settings;
82
		}
83
	};
84
85
86
	var settings = new Settings(OC.generateUrl('apps/passman/api/v2/settings'));
87
	settings.load();
88
	// ADMIN SETTINGS
89
90
	// fill the boxes
91
	$('#passman_link_sharing_enabled').prop('checked', (settings.getKey('link_sharing_enabled').toString().toLowerCase() === '1'));
92
	$('#passman_sharing_enabled').prop('checked', (settings.getKey('user_sharing_enabled').toString().toLowerCase() === '1'));
93
	$('#passman_check_version').prop('checked', (settings.getKey('check_version').toString().toLowerCase() === '1'));
94
	$('#passman_https_check').prop('checked', (settings.getKey('https_check').toString().toLowerCase() === '1'));
95
	$('#passman_disable_contextmenu').prop('checked', (settings.getKey('disable_contextmenu').toString().toLowerCase() === '1'));
96
	$('#passman_disable_debugger').prop('checked', (settings.getKey('disable_debugger').toString().toLowerCase() === '1'));
97
	$('#vault_key_strength').val(settings.getKey('vault_key_strength'));
98
99
100
	$('#passman_check_version').change(function () {
101
		settings.setAdminKey('check_version', ($(this).is(":checked")) ? 1 : 0);
102
	});
103
104
	$('#passman_https_check').change(function () {
105
		settings.setAdminKey('https_check', ($(this).is(":checked")) ? 1 : 0);
106
	});
107
108
	$('#passman_disable_contextmenu').change(function () {
109
		settings.setAdminKey('disable_contextmenu', ($(this).is(":checked")) ? 1 : 0);
110
	});
111
112
	$('#passman_disable_debugger').change(function () {
113
		settings.setAdminKey('disable_debugger', ($(this).is(":checked")) ? 1 : 0);
114
	});
115
116
	$('#passman_sharing_enabled').change(function () {
117
		settings.setAdminKey('user_sharing_enabled', ($(this).is(":checked")) ? 1 : 0);
118
	});
119
120
	$('#passman_link_sharing_enabled').change(function () {
121
		settings.setAdminKey('link_sharing_enabled', ($(this).is(":checked")) ? 1 : 0);
122
	});
123
	$('#vault_key_strength').change(function () {
124
		settings.setAdminKey('vault_key_strength', $(this).val());
125
	});
126
127
	if ($('form[name="passman_settings"]').length === 2) {
128
		$('form[name="passman_settings"]')[1].remove();
129
	}
130
131
	var accountMover = {
132
		'source_account': '',
133
		'destination_account': ''
134
	};
135
	$(".username-autocomplete").autocomplete({
136
		source: OC.generateUrl('apps/passman/admin/search'),
137
		minLength: 1,
138
		select: function (event, ui) {
139
			accountMover[$(this).attr('id')] = ui.item.value;
140
		}
141
	});
142
143
	$('#move_credentials').click(function () {
144
		var self = this;
145
		$('#moveStatus').hide();
146
		$(self).attr('disabled', 'disabled');
147
		$(self).html('<i class="fa fa-spinner fa-spin"></i> ' + OC.L10N.translate('passman', 'Moving') + '...');
148
		if (accountMover.source_account && accountMover.destination_account) {
149
			$.post(OC.generateUrl('apps/passman/admin/move'), accountMover, function (data) {
150
				if (data.success) {
151
					$(self).removeAttr('disabled');
152
					$(self).html('Move');
153
					$('#moveStatus').fadeIn();
154
					setTimeout(function () {
155
						$('#moveStatus').fadeOut();
156
					}, 3500);
157
				}
158
			});
159
		}
160
	});
161
162
	function format_date(date) {
163
		date = new Date(date);
164
		var month=date.getMonth();
165
		var year=date.getFullYear();
166
		var day=date.getDate();
167
		var hour=date.getHours();
168
		var minutes=date.getMinutes();
169
		var seconds=date.getSeconds();
170
171
		month=month+1; //javascript date goes from 0 to 11
172
		if (month<10){
173
			month="0"+month; //adding the prefix
174
		}
175
		if (hour<10){
176
			hour="0"+hour; //adding the prefix
177
		}
178
		if (minutes<10){
179
			minutes="0"+minutes; //adding the prefix
180
		}
181
		if (seconds<10){
182
			seconds="0"+seconds; //adding the prefix
183
		}
184
185
186
187
		return day+"-"+month+"-"+year+" "+hour+":"+minutes+":"+seconds;
188
	}
189
190
	function acceptDeleteRequest (el, req) {
191
		if (!confirm(OC.L10N.translate('passman', "Are you really sure?\nThis will delete the vault and all credentials in it!"))) {
192
			return;
193
		}
194
		$.post(OC.generateUrl('apps/passman/admin/accept-delete-request'), req, function () {
195
			$(el).parent().parent().remove();
196
		});
197
	}
198
199
	function ignoreDeleteRequest (el, req) {
200
		$.ajax({
201
			url: OC.generateUrl('apps/passman/admin/request-deletion/' + req.vault_guid),
202
			type: 'DELETE',
203
			success: function () {
204
				$(el).parent().parent().remove();
205
			}
206
		});
207
	}
208
209
	$.get(OC.generateUrl('apps/passman/admin/delete-requests'), function (requests) {
210
		var table = $('#requests-table tbody');
211
		$.each(requests, function (k, request) {
212
			var accept = $('<span class="link">[Accept]&nbsp;</span>');
213
			accept.click(function () {
214
				var _self = this;
215
				acceptDeleteRequest(_self, request);
216
			});
217
218
			var ignore = $('<span class="link">[Ignore]</span>');
219
			ignore.click(function () {
220
				var _self = this;
221
				ignoreDeleteRequest(_self, request);
222
			});
223
224
			var cols = $('<td>' + request.id + '</td><td>' + request.displayName + '</td><td>' + request.reason + '</td><td>' + format_date(request.created * 1000 )+ '</td>');
225
			var actions = $('<td></td>').append(accept).append(ignore);
226
			table.append($('<tr></tr>').append(cols).append(actions));
227
		});
228
	});
229
230
	$('#passman-tabs').tabs();
231
});
232